/*
==========================================================================
File:        ex2.c (skeleton)
Version:     4, 20/1/2011
Authors:     Toby Howard
==========================================================================
*/

/* The following ratios are not to scale: */
/* Moon orbit : planet orbit */
/* Orbit radius : body radius */
/* Sun radius : planet radius */

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "frames.h"

#define MAX_BODIES 20
#define TOP_VIEW 1
#define ECLIPTIC_VIEW 2
#define SHIP_VIEW 3
#define EARTH_VIEW 4
#define PI 3.14159
#define DEG_TO_RAD 0.017453293
#define ORBIT_POLY_SIDES 40
#define TIME_STEP 0.5   /* days per frame */
#define RUN_SPEED  3000000
#define TURN_ANGLE 1.0

GLfloat  eyex,    eyey,    eyez;    /* Eye point                */
GLfloat  centerx, centery, centerz; /* Look point               */
GLfloat  upx,     upy,     upz;     /* View up vector           */
GLdouble lat,     lon;
GLdouble mlat,    mlon;
GLint width= 640, height= 480;

typedef struct { 
  char    name[20];       /* name */
  GLfloat r, g, b;        /* colour */
  GLfloat orbital_radius; /* distance to parent body (km) */
  GLfloat orbital_tilt;   /* angle of orbit wrt ecliptic (deg) */
  GLfloat orbital_period; /* time taken to orbit (days) */
  GLfloat radius;         /* radius of body (km) */
  GLfloat axis_tilt;      /* tilt of axis wrt body's orbital plane (deg) */
  GLfloat rot_period;     /* body's period of rotation (days) */
  GLint   orbits_body;    /* identifier of parent body */
  GLfloat spin;           /* current spin value (deg) */
  GLfloat orbit;          /* current orbit value (deg) */
 } body;

body  bodies[MAX_BODIES];
int   numBodies, current_view, draw_labels, draw_orbits, draw_starfield, draw_axis;

int   notMoved, viewSet;

int   plotStars;  

typedef struct{
    GLint x;
    GLint y;
    GLint z;
} stars;

stars theStarField[100000];

float date;               
/*****************************/

float myRand (void)
{
  /* return a random float in the range [0,1] */
	//printf("%f\n", (float) rand() / RAND_MAX);
  return (float) rand() / RAND_MAX;
}

/*****************************/

void drawStarfield (void)
{
  /* This is for you to complete. */
  int i;
  if(plotStars){	
    GLint a,b,c,a1,b1,c1,a2,b2,c2,a3,b3,c3,a4,b4,c4,a5,b5,c5,a6,b6,c6,a7,b7,c7;
    for(i= 0 ; i< 12500; i+=8){
        a = -900000000 * myRand(); 
        b = 900000000 * myRand(); 
        c = -900000000 * myRand();

        a1 = 900000000 * myRand(); 
        b1 = 900000000 * myRand(); 
        c1 = 900000000 * myRand();

        a2 = -900000000 * myRand(); 
        b2 = 900000000 * myRand(); 
        c2 = 900000000 * myRand(); 

        a3 = 900000000 * myRand(); 
        b3 = 900000000 * myRand(); 
        c3 = -900000000 * myRand();
     // upper 4 spaces
     // lower 4 spaces
        a4 = -900000000 * myRand(); 
        b4 = -900000000 * myRand(); 
        c4 = -900000000 * myRand();

        a5 = 900000000 * myRand(); 
        b5 = -900000000 * myRand(); 
        c5 = 900000000 * myRand();

        a6 = -900000000 * myRand(); 
        b6 = -900000000 * myRand(); 
        c6 = 900000000 * myRand(); 

        a7 = 900000000 * myRand(); 
        b7 = -900000000 * myRand(); 
        c7 = -900000000 * myRand();
		
	theStarField[i].x = a;
	theStarField[i].y = b;
	theStarField[i].z = c;
	
	theStarField[i+1].x = a1;
	theStarField[i+1].y = b1;
	theStarField[i+1].z = c1;
	
	theStarField[i+2].x = a2;
	theStarField[i+2].y = b2;
	theStarField[i+2].z = c2;
	
	theStarField[i+3].x = a3;
	theStarField[i+3].y = b3;
	theStarField[i+3].z = c3;
	
	theStarField[i+4].x = a4;
	theStarField[i+4].y = b4;
	theStarField[i+4].z = c4;
	
	theStarField[i+5].x = a5;
	theStarField[i+5].y = b5;
	theStarField[i+5].z = c5;
		
	theStarField[i+6].x = a6;
	theStarField[i+6].y = b6;
	theStarField[i+6].z = c6;
		
	theStarField[i+7].x = a7;
	theStarField[i+7].y = b7;
	theStarField[i+7].z = c7;

   }
   plotStars = !plotStars;
 }
 /* and all the other points */
 int j;  
 glBegin (GL_POINTS);		
   for(j = 0; j<100000; j++){
	//printf("%d %d %d \n", theStarField[j].x, theStarField[j].y, theStarField[j].z);
	glVertex3f(theStarField[j].x, theStarField[j].y, theStarField[j].z);
   }
 glEnd ();
	 
}

/*****************************/

void readSystem(void)
{
  /* reads in the description of the solar system */

  FILE *f;
  int i;

  f= fopen("sys", "r");
  if (f == NULL) {
     printf("ex2.c: Couldn't open the datafile 'sys'\n");
     printf("To get this file, use the following command:\n");
     printf("  cp /opt/info/courses/COMP27112/ex2/sys .\n");
     exit(0);
  }
  fscanf(f, "%d", &numBodies);
  for (i= 0; i < numBodies; i++)
  {
    fscanf(f, "%s %f %f %f %f %f %f %f %f %f %d", 
      bodies[i].name,
      &bodies[i].r, &bodies[i].g, &bodies[i].b,
      &bodies[i].orbital_radius,
      &bodies[i].orbital_tilt,
      &bodies[i].orbital_period,
      &bodies[i].radius,
      &bodies[i].axis_tilt,
      &bodies[i].rot_period,
      &bodies[i].orbits_body);

    /* Initialise the body's state */
    bodies[i].spin= 0.0;
    bodies[i].orbit= myRand() * 360.0; /* Start each body's orbit at a
                                          random angle */
    bodies[i].radius*= 1000.0; /* Magnify the radii to make them visible */
  }
  fclose(f);
}

/*****************************/

void drawString (void *font, float x, float y, char *str)
{ /* Displays the string "str" at (x,y,0), using font "font" */

  /* This is for you to complete. */
  char *ch;
  glRasterPos3f(x, y, 0.0);
  for (ch = str; *ch; ch++)
      glutBitmapCharacter(font, (int)*ch);
}

/*****************************/

void showDate(void)
{ /* Displays the current date */

  /* This is for you to complete. */
  
}

/*****************************/
void calculate_lookpoint(void) { 	
    
	/*GLfloat dir_x = cos((lat+mlat)*DEG_TO_RAD)*sin((lon+mlon)*DEG_TO_RAD);
	GLfloat dir_y = sin((lat+mlat)*DEG_TO_RAD);
	GLfloat dir_z = cos((lat+mlat)*DEG_TO_RAD)*cos((lon+mlon)*DEG_TO_RAD);*/
	
        GLfloat dir_x = cos((lat)*DEG_TO_RAD)*sin((lon)*DEG_TO_RAD);
	GLfloat dir_y = sin((lat)*DEG_TO_RAD);
	GLfloat dir_z = cos((lat)*DEG_TO_RAD)*cos((lon)*DEG_TO_RAD);
	//printf("LAN LON: %f %f %f %f\n", lat, mlat, lon, mlon);
	
	//printf("eye point: %f %f %f\n", eyex, eyey, eyez);
	printf("direction: %f %f %f\n", dir_x, dir_y, dir_z);
        //printf("center BF: %f %f %f\n", centerx, centery, centerz);
        printf("\n");
       
       //if(current_view == SHIP_VIEW){
          centerx = (eyex  + (dir_x*10000.0));
	  centery = (eyey  + (dir_y*10000.0)); 
	  centerz = (eyez  + (dir_z*10000.0)); 
       /*}
       else if(current_view == ECLIPTIC_VIEW){
           centerx = (eyex + dir_x);
           centery = (eyey + dir_y);
           centerz = (eyez + dir_z) - (float)pow(10,8);
       }
       else if(current_view == TOP_VIEW){
           centerx = (eyex + dir_x);
           centery = (eyey + dir_y) - (float)pow(10,8);
           centerz = (eyez + dir_z);
       }*/

       //printf("center AF: %f %f %f\n", centerx, centery, centerz);
} // calculate_lookpoint()

//****************************/
void setView (void) {
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  switch (current_view) {
  case TOP_VIEW:
    /* This is for you to complete. */
      if(viewSet || notMoved){
         eyez = eyex = 0;
         eyey = 6*pow(10,8);
         upy = upx = 0;
         upz = -1;
         viewSet = !viewSet;
      }
      else if (!notMoved){
	    calculate_lookpoint();
      }
    break;  
  case ECLIPTIC_VIEW:
       //printf("I can't go into viewSet ");
       if(viewSet || notMoved){
	  //printf("before ECLIPTIC_VIEW: %f %f %f\n", eyex, eyey, eyez);
          eyey = eyex = 0;
          eyez = 5*pow(10,8);
	  upz = upx = 0;
	  upy = 1;
	  //printf("after ECLIPTIC_VIEW: %f %f %f\n", eyex, eyey, eyez);
	  viewSet = !viewSet;
	  //printf("\nviewSet has been set false\n");
	}
	else if (!notMoved){
	    calculate_lookpoint();
	}
    break; 
  case SHIP_VIEW:
       if(viewSet || notMoved){
          eyey = 100000000;
          eyex = 200000000;
          eyez = 200000000;
          upx = 0;//-1;
          upz = 0;//-1;
          upy = 1;
          viewSet = !viewSet;
      }
      else if (!notMoved){
           calculate_lookpoint();
      }
      /* This is for you to complete. */    
    break;  
  case EARTH_VIEW: 
      eyex = cos((bodies[3].orbital_tilt)*DEG_TO_RAD) * (1.15*bodies[3].orbital_radius) * cos(bodies[3].orbit*DEG_TO_RAD); 
      eyey = sin((bodies[3].orbital_tilt)*DEG_TO_RAD) * (1.15*bodies[3].orbital_radius) * cos(bodies[3].orbit*DEG_TO_RAD)+13000000; 
      eyez = cos((bodies[3].orbital_tilt)*DEG_TO_RAD) * (1.15*bodies[3].orbital_radius) * sin(bodies[3].orbit*DEG_TO_RAD);
    upx = 0;
    upz = 0;
    upy = 1;
    /* This is for you to complete. */
    break;  
  }
}

/*****************************/

void menu (int menuentry) {
  switch (menuentry) {
  case 1: current_view= TOP_VIEW;
          if(!viewSet){
	      viewSet = !viewSet;
              centerx=centery=centerz=0;
              if(!notMoved){
              notMoved = !notMoved;}
          }
          break;
  case 2: current_view= ECLIPTIC_VIEW;
	  if(!viewSet){
	      viewSet = !viewSet;
              centerx=centery=centerz=0;
              if(!notMoved){
              notMoved = !notMoved;}
          }
          break;
  case 3: current_view= SHIP_VIEW;
	  if(!viewSet){
	      viewSet = !viewSet;
              centerx=centery=centerz=0;
              if(!notMoved){
              notMoved = !notMoved;}
          }
          break;
  case 4: current_view= EARTH_VIEW;
	  if(!viewSet){
	      viewSet = !viewSet;
              centerx=centery=centerz=0;
              if(!notMoved){
              notMoved = !notMoved;}
          }
          break;
  case 5: draw_labels= !draw_labels;
          break;
  case 6: draw_orbits= !draw_orbits;
          break;
  case 7: draw_starfield= !draw_starfield;
          break;
  case 8: draw_axis= !draw_axis;
          break;
  case 9: exit(0);
  }
}

/*****************************/
void drawAxis()
{
  glPushMatrix();  
  //glRotatef(90, 1.0, 0.0, 0.0);
  glColor3f(0.0,0.0,5.0); // z-axis blue 
  glBegin(GL_LINE_STRIP);
       glVertex3f(0.0,0.0,10*bodies[0].radius);
       glVertex3f(0.0,0.0,-10*bodies[0].radius); 
  glEnd();

  glColor3f(5.0,0.0,0.0); // y-axis red
  glBegin(GL_LINE_STRIP);
       glVertex3f(0.0,10*bodies[0].radius,0.0);
       glVertex3f(0.0,-10*bodies[0].radius,0.0); 
  glEnd();

  glColor3f(0.0,5.0,0.0); // x-axis green
  glBegin(GL_LINE_STRIP);
       glVertex3f(10*bodies[0].radius,0.0,0.0);
       glVertex3f(-10*bodies[0].radius,0.0,0.0); 
  glEnd();
  
  glPopMatrix();  
}

void init(void)
{
  /* Define background colour */
  glClearColor(0.0, 0.0, 0.0, 0.0);
  
  eyex=  0.0; /* Set eyepoint at eye height within the scene */
  eyey=  1.7;
  eyez= 10.0;

  upx= 0.0;   /* Set up direction to the +Y axis  */
  upy= 10.0;
  upz= 0.0;
	
  lat = 0.0;
  lon = 0.0; 
  mlat = 0.0;
  mlon = 0.0;

  //centerx=centery=centerz=0;
  
  glutCreateMenu (menu);
  glutAddMenuEntry ("Top view", 1);
  glutAddMenuEntry ("Ecliptic view", 2);
  glutAddMenuEntry ("Spaceship view", 3);
  glutAddMenuEntry ("Earth view", 4);
  glutAddMenuEntry ("", 999);
  glutAddMenuEntry ("Toggle labels", 5);
  glutAddMenuEntry ("Toggle orbits", 6);
  glutAddMenuEntry ("Toggle starfield", 7);
  glutAddMenuEntry ("Toggle Axis", 8);
  glutAddMenuEntry ("", 999);
  glutAddMenuEntry ("Quit", 9);
  glutAttachMenu (GLUT_RIGHT_BUTTON);

  current_view= TOP_VIEW;
  draw_labels= 1;
  draw_orbits= 1;
  draw_starfield= 1;
  draw_axis = 1;
	
  plotStars = 1;
  viewSet = 1;
  notMoved = 1;

}

/*****************************/

void animate(void)
{
  int i;

    date+= TIME_STEP;

    for (i= 0; i < numBodies; i++)  {
      bodies[i].spin += 360.0 * TIME_STEP / bodies[i].rot_period;
      bodies[i].orbit += 360.0 * TIME_STEP / bodies[i].orbital_period;
      glutPostRedisplay();
    }

}

/*****************************/

void drawOrbit (int n)
{ /* Draws a polygon to approximate the circular
     orbit of body "n" */
    int i;
    int in = 360 / ORBIT_POLY_SIDES;
    /* This is for you to complete. */
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    //glBegin(GL_POLYGON);
    
    glBegin(GL_LINE_LOOP);
       for(i=0; i<360; i++){
           glVertex3f(cos(bodies[n].orbital_tilt*DEG_TO_RAD) * cos(i*in*DEG_TO_RAD) * bodies[n].orbital_radius, 
                      sin(bodies[n].orbital_tilt*DEG_TO_RAD) * cos(i*in*DEG_TO_RAD) * bodies[n].orbital_radius, 
                      cos(bodies[n].orbital_tilt*DEG_TO_RAD) * sin(i*in*DEG_TO_RAD) * bodies[n].orbital_radius);
       }
    glEnd ();    
}

/*****************************/

void drawLabel(int n)
{ /* Draws the name of body "n" */

    /* This is for you to complete. */
 glPushMatrix();    
   glTranslatef(cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                sin(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * sin(bodies[n].orbit*DEG_TO_RAD));

   //glColor3f(1.0,1.0,1.0);
   drawString(GLUT_BITMAP_HELVETICA_18, 0, 1.2*bodies[n].radius, bodies[n].name);
 glPopMatrix();
}

/*****************************/

void drawBody(int n)
{
 /* Draws body "n" */
    /* This is for you to complete. */
  glColor3f(bodies[n].r, bodies[n].g, bodies[n].b);
  
  if( n == 5){  // Moon
   
    glPushMatrix(); 

      //Earth Torb & Totilt
      glTranslatef(cos(bodies[3].orbital_tilt*DEG_TO_RAD) * bodies[3].orbital_radius * cos(bodies[3].orbit*DEG_TO_RAD), 
                   sin(bodies[3].orbital_tilt*DEG_TO_RAD) * bodies[3].orbital_radius * cos(bodies[3].orbit*DEG_TO_RAD), 
                   cos(bodies[3].orbital_tilt*DEG_TO_RAD) * bodies[3].orbital_radius * sin(bodies[3].orbit*DEG_TO_RAD));
      //Earth Tatilt
      //glRotatef(bodies[3].axis_tilt, 1.0, 1.0, 1.0);  
 
      if(draw_orbits){
        drawOrbit(n);}
 
      if(draw_labels){
        drawLabel(n);}
      //Torb & Totilt
      glTranslatef(cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                      sin(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                      cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * sin(bodies[n].orbit*DEG_TO_RAD));
      //Tatilt
      glRotatef(bodies[n].axis_tilt, 1.0, 1.0, 1.0);
      //Tspin
      glRotatef(bodies[n].spin, 0.0, 1.0, 0.0);
      //Tvert
      glRotatef(90, 1.0, 0.0, 0.0);

      glBegin(GL_LINE_STRIP);
              glVertex3f(0.0,0.0,1.3*bodies[n].radius);
              glVertex3f(0.0,0.0,-1.3*bodies[n].radius); 
      glEnd();

      glutWireSphere((bodies[n].radius), 20, 20);

    glPopMatrix();

  }
  else if( n == 6 || n == 7){

    glPushMatrix(); 

      //Earth Torb & Totilt
      glTranslatef(cos(bodies[4].orbital_tilt*DEG_TO_RAD) * bodies[4].orbital_radius * cos(bodies[4].orbit*DEG_TO_RAD), 
                   sin(bodies[4].orbital_tilt*DEG_TO_RAD) * bodies[4].orbital_radius * cos(bodies[4].orbit*DEG_TO_RAD), 
                   cos(bodies[4].orbital_tilt*DEG_TO_RAD) * bodies[4].orbital_radius * sin(bodies[4].orbit*DEG_TO_RAD));
      //Earth Tatilt
      //glRotatef(bodies[4].axis_tilt, 1.0, 1.0, 1.0);  
 
      if(draw_orbits){
        drawOrbit(n);}
 
      if(draw_labels){
        drawLabel(n);}
      //Torb & Totilt
      glTranslatef(cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                   sin(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                   cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * sin(bodies[n].orbit*DEG_TO_RAD));
      //Tatilt
      glRotatef(bodies[n].axis_tilt, 1.0, 1.0, 1.0);
      //Tspin
      glRotatef(bodies[n].spin, 0.0, 1.0, 0.0);
      //Tvert
      glRotatef(90, 1.0, 0.0, 0.0);

      glBegin(GL_LINE_STRIP);
              glVertex3f(0.0,0.0,1.3*bodies[n].radius);
              glVertex3f(0.0,0.0,-1.3*bodies[n].radius); 
      glEnd();

      glutWireSphere((bodies[n].radius), 20, 20);

    glPopMatrix();
  }
  else{
   
   glPushMatrix();
  
     if(draw_orbits){
        drawOrbit(n);}
 
     if(draw_labels){
        drawLabel(n);}
   
      if( n != 0){

         //Torb & Totilt
         glTranslatef(cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                      sin(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * cos(bodies[n].orbit*DEG_TO_RAD), 
                      cos(bodies[n].orbital_tilt*DEG_TO_RAD) * bodies[n].orbital_radius * sin(bodies[n].orbit*DEG_TO_RAD));
         //Tatilt
         glRotatef(bodies[n].axis_tilt, 1.0, 1.0, 1.0);  
         //Tspin
         glRotatef(bodies[n].spin, 0.0, 1.0, 0.0);
          
      }
      //Tvert
      glRotatef(90, 1.0, 0.0, 0.0);

      glBegin(GL_LINE_STRIP);
              glVertex3f(0.0,0.0,1.3*bodies[n].radius);
              glVertex3f(0.0,0.0,-1.3*bodies[n].radius); 
      glEnd();
     
      glutWireSphere((bodies[n].radius), 20, 20);

    glPopMatrix();
  }
  
}

/*****************************/

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
 
  frameStart();
     int i;

     /* set the camera */

     setView();
     //printf("Final Center: %f %f %f\n", centerx, centery, centerz);
     //printf("\n////////////////////////////////////////////////\n");
     gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz);

     if (draw_starfield){
         drawStarfield();}
     if (!draw_axis){
         drawAxis();}

     for (i= 0; i < numBodies; i++)
     {
          glPushMatrix();
          drawBody (i);
          glPopMatrix();
     }

  frameEnd(GLUT_BITMAP_HELVETICA_10, 1.0, 1.0, 1.0, 0.05, 0.95);

  glutSwapBuffers();
}

/*****************************/

void reshape(int w, int h)
{
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective (48.0, (GLfloat) w/(GLfloat) h, 10000.0, 800000000.0);
  width= w;   /* Record the new width and height */
  height= h;
}
  
/*****************************/
void mouse_motion(int x, int y) {
	
	/* To be completed*/ 
	mlon = (-100.0/width)*x + 50;
	mlat = (-100.0/height)*y + 50; 
	
} // mouse_motion()
void keyboard(unsigned char key, int x, int y)
{
	switch (key)
	{    
	   case 27:  /* Escape key */
	    exit(0);
	}
} 
void cursor_keys(int key, int x, int y) {
	switch (key) {
		GLfloat updirx;
		GLfloat updirz;
		GLfloat downdirx;
		GLfloat downdirz;
		
		/* To be completed */
	case GLUT_KEY_LEFT:
                if(notMoved){
                   notMoved = !notMoved;
                }
		lon = lon + TURN_ANGLE;
		break;
	
	case GLUT_KEY_RIGHT:
                if(notMoved){
                   notMoved = !notMoved;
                }
		lon = lon - TURN_ANGLE;
		break;
		
	case GLUT_KEY_PAGE_UP:
                if(notMoved){
                   notMoved = !notMoved;
                }
		lat = lat + TURN_ANGLE;
		
		if(lat >= 90){
			lat = 89;
		}
		else if(lat <= -90){
			lat = -89;
		}
		
		break;
		
	case GLUT_KEY_PAGE_DOWN:
                if(notMoved){
                   notMoved = !notMoved;
                }
		lat = lat - TURN_ANGLE;
		
		if(lat >= 90){
			lat = 89;
		}
		else if(lat <= -90){
			lat = -89;
		}
		
		break;
		
	case GLUT_KEY_HOME:
		init();
		break;
		
	case GLUT_KEY_UP:
		if(notMoved){
                   notMoved = !notMoved;
                }
		updirx = RUN_SPEED * sin(lon*DEG_TO_RAD);
		updirz = RUN_SPEED * cos(lon*DEG_TO_RAD);
		
                printf("direction : %f %f \n", updirx, updirz);
                printf("before movement : %f %f \n", eyex, eyez);
		
                eyex = eyex + updirx; 
		eyez = eyez + updirz;

                printf("after movement : %f %f \n", eyex, eyez);
                printf("\n");
		break;
		
		
	case GLUT_KEY_DOWN:
		if(notMoved){
                   notMoved = !notMoved;
                }
		downdirx = RUN_SPEED * sin(lon*DEG_TO_RAD);
		downdirz = RUN_SPEED * cos(lon*DEG_TO_RAD);
		
                printf("direction : %f %f \n", downdirx, downdirx);
                printf("before movement : %f %f \n", eyex, eyez);

		eyex = eyex - downdirx; 
		eyez = eyez - downdirz;

                printf("after movement : %f %f \n", eyex, eyez);
                printf("\n");
		break;
		
   }
} // cursor_keys()
/*****************************/

int main(int argc, char** argv)
{
  glutInit (&argc, argv);
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  glutCreateWindow ("COMP27112 Exercise 2");
  glutFullScreen();
  init ();
  glutDisplayFunc (display); 
  glutReshapeFunc (reshape);
  glutKeyboardFunc (keyboard);
  glutSpecialFunc (cursor_keys);
  glutPassiveMotionFunc (mouse_motion);
  glutIdleFunc (animate);
  readSystem();
  glutMainLoop ();
  return 0;
}
/* end of ex2.c */
